﻿Imports DTO
Imports BUS
Imports DevExpress.XtraEditors
Imports System.IO

Public Class frmThemHangHoa

    Public Delegate Sub ThemNV(nv As HangHoaDTO)

    Public Event LamMoiNVKhiThem As ThemNV

    Public kt As Integer = 0

    Public mahh As String
    Public tenhh As String
    Public gm As Double
    Public gbs As Double
    Public gbl As Double
    Public toithieu As Double
    Public matt As Integer

    Public lhh As Integer
    Public kmd As String
    Public nh As String
    Public dv As String
    Public ncc As String
    Public anh As Byte()


    Dim hanghoa As HangHoaDTO

    Private Sub cbxKhoMD_Properties_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles cbxKhoMD.Properties.ButtonClick
        If e.Button.Tag.ToString() = "cbxbtnThemKho" Then
            Dim frm As New frmThemKhoHang
            frm.ShowDialog()
        End If
    End Sub

    Private Sub LoadCbxKhohang()
        cbxKhoMD.Properties.DataSource = KhoHangBUS.LoadBangKhoHang()
        cbxKhoMD.Properties.ValueMember = "MaKhoHang"
        cbxKhoMD.Properties.DisplayMember = "TenKhoHang"
        SetEditValueByIndexKH(cbxKhoMD, 0)
    End Sub

    'hàm có chức năng selectedindex = 0
    Private Sub SetEditValueByIndexKH(ByVal edit As GridLookUpEdit, ByVal index As Integer)
        Dim keyValue As Object = cbxKhoMD.Properties.GetKeyValue(index)
        cbxKhoMD.EditValue = keyValue
    End Sub

    Private Sub cbxPhanLoaiHH_Properties_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles cbxPhanLoaiHH.Properties.ButtonClick
        If e.Button.Tag.ToString() = "cbxbtnThemPhanLoai" Then
            Dim frm As New frmThemNhomHang
            frm.ShowDialog()
        End If
    End Sub

    Private Sub LoadCbxNhomHang()
        cbxPhanLoaiHH.Properties.DataSource = NhomHangBUS.LayDSNhomHangCbx()
        cbxPhanLoaiHH.Properties.ValueMember = "MaNhomHang"
        cbxPhanLoaiHH.Properties.DisplayMember = "TenNhomHang"
        SetEditValueByIndexNH(cbxPhanLoaiHH, 0)
    End Sub

    'hàm có chức năng selectedindex = 0
    Private Sub SetEditValueByIndexNH(ByVal edit As GridLookUpEdit, ByVal index As Integer)
        Dim keyValue As Object = cbxPhanLoaiHH.Properties.GetKeyValue(index)
        cbxPhanLoaiHH.EditValue = keyValue
    End Sub


    Private Sub LoadCbxLoaiHH()
        cbxLoaiHH.Properties.DataSource = HangHoaBUS.LayDSLoaiHHCbx()
        cbxLoaiHH.Properties.ValueMember = "Ma"
        cbxLoaiHH.Properties.DisplayMember = "TenLoaiHangHoa"
        SetEditValueByIndex(cbxLoaiHH, 0)
    End Sub

    'hàm có chức năng selectedindex = 0
    Private Sub SetEditValueByIndex(ByVal edit As GridLookUpEdit, ByVal index As Integer)
        Dim keyValue As Object = cbxLoaiHH.Properties.GetKeyValue(index)
        cbxLoaiHH.EditValue = keyValue
    End Sub


    Private Sub cbxDonViHH_Properties_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles cbxDonViHH.Properties.ButtonClick
        If e.Button.Tag.ToString() = "cbxbtnThemDonVi" Then
            Dim frm As New frmThemDonVi
            frm.ShowDialog()
        End If
    End Sub

    Private Sub LoadCbxDonVi()
        cbxDonViHH.Properties.DataSource = DonViBUS.LayDSDonViCbx()
        cbxDonViHH.Properties.ValueMember = "MaDonVi"
        cbxDonViHH.Properties.DisplayMember = "TenDonVi"
        SetEditValueByIndexDV(cbxDonViHH, 0)
    End Sub

    Private Sub SetEditValueByIndexDV(ByVal edit As GridLookUpEdit, ByVal index As Integer)
        Dim keyValue As Object = cbxDonViHH.Properties.GetKeyValue(index)
        cbxDonViHH.EditValue = keyValue
    End Sub

    Private Sub cbxNhaCCHH_Properties_ButtonClick(sender As Object, e As DevExpress.XtraEditors.Controls.ButtonPressedEventArgs) Handles cbxNhaCCHH.Properties.ButtonClick
        If e.Button.Tag.ToString() = "cbxbtnThemNhaCungCap" Then
            Dim frm As New frmThemNhaCungCap
            frm.ShowDialog()
        End If
    End Sub

    Private Sub LoadCbxNCC()
        cbxNhaCCHH.Properties.DataSource = NhaCungCapBUS.LayDSNhaCungCapCbx()
        cbxNhaCCHH.Properties.ValueMember = "MaNhaCungCap"
        cbxNhaCCHH.Properties.DisplayMember = "TenNhaCungCap"
        SetEditValueByIndexNCC(cbxNhaCCHH, 0)
    End Sub

    'hàm có chức năng selectedindex = 0
    Private Sub SetEditValueByIndexNCC(ByVal edit As GridLookUpEdit, ByVal index As Integer)
        Dim keyValue As Object = cbxNhaCCHH.Properties.GetKeyValue(index)
        cbxNhaCCHH.EditValue = keyValue
    End Sub


    Private Sub frmThemHangHoa_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        LoadCbxKhohang()
        LoadCbxLoaiHH()
        LoadCbxNhomHang()
        LoadCbxDonVi()
        LoadCbxNCC()
        If kt = 1 Then
            txtMaHangHH.Text = mahh
            txtTenHangHH.Text = tenhh
            calGiaMuaHH.Text = gm.ToString()
            calGiaBanLeHH.Text = gbl.ToString()
            calGiaBanSiHH.Text = gbs.ToString()
            calTonKhoMinHH.Text = toithieu.ToString()
            picHinhHH.Image = byteArrayToImage(anh)
            If matt = 1 Then
                chkConQuanLy.Checked = True
            Else
                chkConQuanLy.Checked = False
            End If
            HangHoaBUS.LayMaHangHoaCbx(mahh, lhh, kmd, nh, dv, ncc)
            cbxDonViHH.EditValue = dv
            cbxKhoMD.EditValue = kmd
            cbxNhaCCHH.EditValue = ncc
            cbxPhanLoaiHH.EditValue = nh
            cbxLoaiHH.EditValue = lhh
        End If
    End Sub

    Public Function imageToByteArray(imageIn As System.Drawing.Image) As Byte()
        Dim ms As New MemoryStream()
        imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
        Return ms.ToArray()
    End Function

    Public Function byteArrayToImage(byteArrayIn As Byte()) As Image
        Dim ms As New MemoryStream(byteArrayIn)
        Dim returnImage As Image = Image.FromStream(ms)
        Return returnImage
    End Function


    Sub ThemHangHoa()
        KhuVucBUS.DongKetNoi.Close()
        Dim hh As New HangHoaDTO

        hh.TenHangHoa = txtTenHangHH.Text.Trim()
        hh.DiaChiAnh = imageToByteArray(picHinhHH.Image)
        hh.GiaBanLe = Convert.ToDouble(calGiaBanLeHH.Text.Trim())
        hh.GiaBanSi = Convert.ToDouble(calGiaBanSiHH.Text.Trim())
        hh.GiaMua = Convert.ToDouble(calGiaMuaHH.Text.Trim())
        hh.MaKhoHang = cbxKhoMD.EditValue.ToString()
        hh.MaLoaiHangHoa = Convert.ToInt32(cbxLoaiHH.EditValue)
        hh.MaNhaCungCap = cbxNhaCCHH.EditValue.ToString()
        hh.MaNhomHang = cbxPhanLoaiHH.EditValue.ToString()
        If chkConQuanLy.Checked = True Then
            hh.MaTrangThai = 1
        Else
            hh.MaTrangThai = 0
        End If
        hh.MaDonVi = cbxDonViHH.EditValue.ToString()
        hh.TonKhoHienTai = Convert.ToDouble(calTonKhoHTHH.Text.Trim())
        hh.TonKhoToiThieu = Convert.ToDouble(calTonKhoMinHH.Text.Trim())
        hh.XuatXu = txtXuatXuHH.Text.Trim()

        If HangHoaBUS.ThemHangHoa(hh) Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Thêm thành công hàng hóa", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Tên hàng hóa không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
        RaiseEvent LamMoiNVKhiThem(hh)
    End Sub

    Sub CapNhatHangHoa()
        KhuVucBUS.DongKetNoi.Close()
        Dim hh As New HangHoaDTO

        hh.MaHangHoa = txtMaHangHH.Text.Trim()
        hh.TenHangHoa = txtTenHangHH.Text.Trim()
        hh.DiaChiAnh = imageToByteArray(picHinhHH.Image)
        hh.GiaBanLe = Convert.ToDouble(calGiaBanLeHH.Text.Trim())
        hh.GiaBanSi = Convert.ToDouble(calGiaBanSiHH.Text.Trim())
        hh.GiaMua = Convert.ToDouble(calGiaMuaHH.Text.Trim())
        hh.MaKhoHang = cbxKhoMD.EditValue.ToString()
        hh.MaLoaiHangHoa = Convert.ToInt32(cbxLoaiHH.EditValue)
        hh.MaNhaCungCap = cbxNhaCCHH.EditValue.ToString()
        hh.MaNhomHang = cbxPhanLoaiHH.EditValue.ToString()
        If chkConQuanLy.Checked = True Then
            hh.MaTrangThai = 1
        Else
            hh.MaTrangThai = 0
        End If
        hh.MaDonVi = cbxDonViHH.EditValue.ToString()
        hh.TonKhoHienTai = Convert.ToDouble(calTonKhoHTHH.Text.Trim())
        hh.TonKhoToiThieu = Convert.ToDouble(calTonKhoMinHH.Text.Trim())
        hh.XuatXu = txtXuatXuHH.Text.Trim()

        If HangHoaBUS.CapNhatHangHoa(hh) Then
            DevExpress.XtraEditors.XtraMessageBox.Show("Cập nhật thành công hàng hóa", "Thông Tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Tên hàng hóa không được để trống", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub btnLuuHH_Click(sender As Object, e As EventArgs) Handles btnLuuHH.Click
        NhanVienBUS.DongKetNoi.Close()
        If kt = 1 Then
            Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 24)
            If pq.Sua = True Then
                HeThongBUS.themNhatKy(frmMain.maND, "Quản lý hàng hóa", "Cập nhật", mahh)
                CapNhatHangHoa()
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        Else
            Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 24)
            If pq.Them = True Then
                HeThongBUS.themNhatKy(frmMain.maND, "Quản lý hàng hóa", "Thêm", mahh)
                ThemHangHoa()
            Else
                DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
        End If
    End Sub

    Private Sub btnDongHH_Click(sender As Object, e As EventArgs) Handles btnDongHH.Click
        Me.Close()
        KhuVucBUS.DongKetNoi.Close()
    End Sub
End Class